package com.learn.springcloud.hystrixexplainindetailconsumer.request.collapser.controller;

import com.learn.springcloud.hystrixexplainindetailconsumer.bean.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Time : 2018/8/6 16:48
 * @Version : 1.0
 */
@RestController
public class RequestCollapserProvider {

    private List<User> users = Arrays.asList(new User(1L, "aaa"),
            new User(2L, "bbb"),
            new User(3L, "ccc"),
            new User(4L, "ddd"),
            new User(5L, "eee"));

    @RequestMapping("/request/collpaser/find/user/{id}")
    public User find(@PathVariable Long id){
        return Optional.ofNullable(users
                .stream()
                .filter(t -> id.equals(t.getId()))
                .collect(Collectors.toList()))
                .orElse(Collections.singletonList(new User(0L, "no this user")))
                .get(0);
    }

    @RequestMapping("/request/collpaser/find/users")
    public List<User> finds(List<Long> ids, HttpServletRequest request){
        System.out.println(request);
        return users
                .stream()
                .filter(t -> ids.contains(t.getId()))
                .collect(Collectors.toList());
    }

}
